import java.util.concurrent.atomic.AtomicInteger;

/**
 *  编写代码, 基于 AtomicInteger 实现多线程自增同一个变量
 * Date: 2022-09-28
 * Time: 16:30
 */
public class Demo3 {

    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(0);

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5_0000; i++) {
                // 相当于 count++;
                atomicInteger.incrementAndGet();
            }
        });


        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5_0000; i++) {
                // 相当于 atomicInteger++;
                atomicInteger.incrementAndGet();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(atomicInteger.get());

    }
}
